import java.io.*;
import java.util.*;

public class C {

    BufferedReader fin;
    PrintWriter fout;

    void openFiles() throws IOException {
        fin = new BufferedReader(new FileReader("C.in"));
        fout = new PrintWriter(new BufferedWriter(new FileWriter("C.out")));
    }

    void closeFiles() throws IOException {
        fin.close();
        fout.close();
    }

    int T, W, Q;
    String[] a;
    int[][] value;
    String result;
    char[] res;
    int total, current, lresult, lres;
    
    void input() throws IOException {
        T = Integer.parseInt(fin.readLine());
        for (int test = 1; test <= T; test++)
        {
            String s = fin.readLine();
            StringTokenizer ss = new StringTokenizer(s);
            W = Integer.parseInt(ss.nextToken());
            Q = Integer.parseInt(ss.nextToken());

            a = new String[W];
            value = new int[W][W];
            for (int j = 0; j < W; j++) 
            {
                a[j] = fin.readLine();
                for (int k = 0; k < W; k++)
                {
                    if (('0' <= a[j].charAt(k)) && (a[j].charAt(k) <= '9'))
                        value[j][k] = Integer.parseInt(new Character(a[j].charAt(k)).toString());
                    else
                        value[j][k] = -1;
                }
            }

            s = fin.readLine();
            ss = new StringTokenizer(s);
            for (int i = 0; i < Q; i++)
            {
                total = Integer.parseInt(ss.nextToken());
                process();
                fout.println("Case #" + i + ": " + result);
            }
        }
    }

    void attempt(int x, int y, char sign)
    {
        if (value[x][y] == -1)
        {
            res[lres++] = a[x].charAt(y);
            attempt(x-1, y, a[x].charAt(y));
            attempt(x+1, y, a[x].charAt(y));
            attempt(x, y-1, a[x].charAt(y));
            attempt(x, y+1, a[x].charAt(y));
            lres--;
        }
        else
        {
            if (sign == '+') current += value[x][y];
            else current -= value[x][y];

            if (current == total)
            {
                if (lres < lresult)
                {
                    result = "";
                    for (int i = 0; i < lres; i++) result += res[i];
                }
            }
        }
    }
    
    void process() {
        lresult = 1000;
        lres = 0;
        res = new char[1000];
        
        for (int i = 0; i < W; i++)
            for (int j = 0; j < W; j++)
            {
                current = 0;
                if (value[i][j] != -1) attempt(i, j, '+');
            }
    }

    void output() {
    }

    public static void main(String[] args) throws IOException {
        C myProgram = new C();

        myProgram.openFiles();
        myProgram.input();
        myProgram.process();
        myProgram.output();
        myProgram.closeFiles();
    }
}
